package net.esj.basic.pojo.op;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import net.esj.basic.core.view.tools.ActionContext;
import net.esj.basic.plugins.freemarker.cache.StringTemplateLoader;
import net.esj.basic.utils.StringUtils;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.struts2.views.freemarker.FreemarkerManager;

public class Operation {

	public static enum OperationTarget{
		Global,ContextMenu
	}
	
	private String id;
	private String title;
	private String icon;
	private String target;
	private String body;
	private String check;
	private int sort;
	private boolean menu;
	
	private String bodyAfterProcess;
	private String checkAfterProcess;
	
	private Collection<Operation> children = new LinkedHashSet<Operation>();
	
	public String getId() {
		if(!StringUtils.hasText(id)){
			id = "_"+RandomStringUtils.random(6, StringUtils.getN36Chars());
		}
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String getBody() {
		return body;
	}
	public String getBodyAfterProcess() {
		if(bodyAfterProcess==null){
			try {
				/*ServletContext servletContext = ServletActionContext.getServletContext();
		        HttpServletRequest request = ServletActionContext.getRequest();
		        HttpServletResponse response = ServletActionContext.getResponse();
		        ValueStack stack = ServletActionContext.getContext().getValueStack();
		        Map map = new HashMap();
		        map.put("servletContext", servletContext);
		        map.put("request", request);
		        map.put("response", response);
		        map.put("stack", stack);*/
				Object object=  ActionContext.getRequest().getAttribute(FreemarkerManager.ATTR_TEMPLATE_MODEL);
				bodyAfterProcess = StringTemplateLoader.process(body,object ).trim();
			} catch (Exception e) {
				e.printStackTrace();
				bodyAfterProcess = null;
			}
		}
		return bodyAfterProcess;
	}
	
	public String getCheckAfterProcess(){
		if(checkAfterProcess==null){
			try {
				Object object=  ActionContext.getRequest().getAttribute(FreemarkerManager.ATTR_TEMPLATE_MODEL);
				checkAfterProcess = StringTemplateLoader.process(check,object ).trim();
			} catch (Exception e) {
				e.printStackTrace();
				checkAfterProcess = null;
			}
		}
		return checkAfterProcess;
	}
	
	public void setBody(String body) {
		this.body = body;
	}
	public int getSort() {
		return sort;
	}
	public void setSort(int sort) {
		this.sort = sort;
	}
	
	public String getCheck() {
		return check;
	}
	public void setCheck(String check) {
		this.check = check;
	}
	public boolean isMenu() {
		return menu;
	}
	public void setMenu(boolean menu) {
		this.menu = menu;
	}
	public boolean equals(Operation op){
		if(org.apache.commons.lang.StringUtils.equals(getTarget(), op.getTarget())
				&& org.apache.commons.lang.StringUtils.equals(getBody(), op.getBody())){
			return true;
		}
		return false;
	}
	
	public Collection<Operation> getChildren() {
		return Collections.unmodifiableSet((Set) children);
	}
	
	public void addChild(Operation op){
		children.add(op);
	}
	
}
